package comment

import (
	"common/models"
	"common/validate"
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"strconv"
	"strings"
	"time"
	"video/pkg/dao/redis"
)

// GetVideAllComment 得到评论- 视频下所有一级评论
// @Summary 获得视频的所有一级评论
// @Description 获得视频的所有一级评论
// @Tags 评论
// @Accept application/json
// @Produce application/json
// @Param object body docModel.DocGetVideAllCommentReq false "搜索参数"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router /comment/video/one/all [post]
func (h CHandler) GetVideAllComment(c *gin.Context) {
	var (
		list        []*models.CommentInfo
		videoInfo   *models.VideoInfo
		commentList []string
		err         error
		commentMap  map[string]string
	)
	commentMap = make(map[string]string)
	resp := new(models.RespData)
	req := new(models.GetVideAllCommentReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Comment:GetVideAllComment with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Comment:GetVideAllComment with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//redis 获得video 的ID
	ID, err := h.R.GetString(redis.VideIdToID + req.VideoId)
	if err != nil {
		// 如果返回的错误是key不存在
		if errors.Is(err, redis.NoKey) {
			videoInfo, err = h.M.VideoInfoQueryByUid(req.VideoId)
			if err != nil {
				zap.L().Error("Comment: GetVideAllComment h.M.VideoInfoQueryByUid err", zap.Error(err))
				resp.SendServerBusy(c)
				return
			}
		}
		// 出其他错了
		zap.L().Error("Comment: GetVideAllComment h.R.GetString err", zap.Error(err))
	}
	if ID != "" {
		videoInfo, err = h.M.VideoInfoQueryById(ID)
		if err != nil {
			zap.L().Error("Comment: GetVideAllComment h.M.VideoInfoQueryById err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
	}
	//查找对应的Comment
	commentList = strings.Split(videoInfo.CommentIds, models.SplitFormat)
	if len(commentList) == 0 {
		resp.SendSuccess(c)
		return
	}
	for _, id := range commentList {
		commentInfo, err := h.M.CommentInfoQueryById(id)
		if err != nil {
			zap.L().Error("Comment: GetVideAllComment h.M.CommentInfoQueryById err", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		commentMap[commentInfo.CommentId] = strconv.Itoa(int(commentInfo.ID))
		list = append(list, commentInfo)
	}
	//将结果存储到redis
	if err := h.R.SetHash(redis.VcID+req.VideoId, commentMap, 10*time.Minute); err != nil {
		zap.L().Error("Comment: GetVideAllComment  h.R.SetHash err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.Data = list
	resp.Count = int64(len(commentList))
	resp.SendSuccess(c)
}

// GetMoreComment 获得多级评论
// @Summary 获得多级评论
// @Description 获得多级评论
// @Tags 评论
// @Accept application/json
// @Produce application/json
// @Param object body docModel.DocGetMoreCommentReq false "搜索参数"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router /comment/video/more/info [post]
func (h CHandler) GetMoreComment(c *gin.Context) {
	var (
		commentID        string
		err              error
		childCommentList []*models.CommentInfo
	)
	resp := new(models.RespData)
	req := new(models.GetMoreCommentReq)
	res := new(models.GetMoreCommentResp)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Comment:GetMoreComment with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Comment:GetMoreComment with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//从redis 获得comment主键ID
	commentID, err = h.R.GetHashToField(redis.VcID+req.VideoId, req.CommentId)
	if err != nil {
		if errors.Is(err, redis.NoKey) {
			commentInfo, err := h.M.CommentInfoQueryByUid(req.CommentId)
			if err != nil {
				zap.L().Error("Comment:GetMoreComment h.M.CommentInfoQueryByUid", zap.Error(err))
				resp.SendServerBusy(c)
				return
			}
			commentID = strconv.Itoa(int(commentInfo.ID))
		}
		zap.L().Error("Comment:GetMoreComment h.R.GetHashToField", zap.Error(err))
	}
	commentInfoP, err := h.M.CommentInfoQueryById(commentID)
	if err != nil {
		zap.L().Error("Comment:GetMoreComment h.M.QueryUserInfoByID", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if req.IsOrChild {
		childListComment := strings.Split(commentInfoP.ChildIds, models.SplitFormat)
		for _, id := range childListComment {
			if id == "" {
				continue
			}
			commentInfo, err := h.M.CommentInfoQueryById(id)
			if err != nil {
				zap.L().Error("Comment:GetMoreComment h.M.CommentInfoQueryById", zap.Error(err))
				resp.SendServerBusy(c)
				return
			}
			childCommentList = append(childCommentList, commentInfo)
		}
	}
	if len(childCommentList) != 0 {
		res.Child = childCommentList
	}
	res.Parent = commentInfoP
	resp.Data = res
	resp.SendSuccess(c)
}
